<?php

/**
 * This is the model class for table "event".
 *
 * The followings are the available columns in table 'event':
 * @property string $id
 * @property string $member_id
 * @property string $category_id
 * @property string $title
 * @property string $content
 * @property string $location
 * @property string $start
 * @property string $end
 * @property string $status
 * @property string $album_id
 * @property string $modified
 * @property string $created
 *
 * The followings are the available model relations:
 * @property Member $member
 * @property Album $album
 * @property Category $category
 * @property Member[] $members
 */
class Event extends CActiveRecord
{
	const STATUS_DELETED = 1;
	const STATUS_PENDING = 3;
	const STATUS_ACTIVE = 5;
	const STATUS_FEATURED = 7;
	
	public $cover;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Event the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'event';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('member_id, category_id, title, content, start, end, status, limit, location', 'required'),
			array('member_id, category_id, album_id', 'length', 'max'=>10),
			array('title, location', 'length', 'max'=>255),
			array('status', 'length', 'max'=>6),
			array('cover', 'file', 'types'=>'jpg,gif,png','allowEmpty'=>true ,'wrongType'=>'只允许 jpg,gif,png'),
			array('member_id, category_id, title, content, start, end, status,limit modified, created', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, member_id, category_id, title, content, location, start, end, status, album_id, modified, created', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'member' => array(self::BELONGS_TO, 'Member', 'member_id'),
			'album' => array(self::BELONGS_TO, 'Album', 'album_id'),
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
			'members' => array(self::MANY_MANY, 'Member', 'user_event_relation(event_id, member_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'member_id' => 'Member',
			'category_id' => '类别',
			'title' => '标题',
			'content' => '内容',
			'location' => '地点',
			'start' => '开始时间',
			'end' => '结束时间',
			'status' => 'Status',
			'album_id' => '相册',
			'limit' => '活动人数',
			'count' => '报名人数',
			'modified' => 'Modified',
			'created' => 'Created',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('member_id',$this->member_id,true);
		$criteria->compare('category_id',$this->category_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('location',$this->location,true);
		$criteria->compare('start',$this->start,true);
		$criteria->compare('end',$this->end,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('album_id',$this->album_id,true);
		$criteria->compare('modified',$this->modified,true);
		$criteria->compare('created',$this->created,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}